from matplotlib import pyplot
import numpy

from histogram.h1d import Histogram1D
from histogram.h2d import Histogram2D
from histogram.h3d import Histogram3D

def now(arr, **kwargs):
    pyplot.clf()

    if not hasattr(arr, 'shape'):
        arr = numpy.array(arr)

    if 'arr2' in kwargs:
        arr2 = kwargs['arr2']
        del kwargs['arr2']

        return now2d(arr, arr2, **kwargs).draw()

    else:
        if len(arr.shape) == 1:
            return now1d(arr).draw()
    
    return None


def now1d(arr, arange=None, nbins=None, title=None, xlabel='Now', xunits='none given'):
    if arange is None:
        arange = (min(arr), max(arr))    

    if nbins is None:
        nbins = bins(arr)

    h = Histogram1D('now1d',
                    nbins=nbins,
                    arange=arange,
                    xunits='none',
                    xlabel='Now')
    
    h.fill(arr)

    return h


def now2d(arr1, arr2, logz=False, arange=None):
    if arange is None:
        arange = ((min(arr1), max(arr1)), (min(arr2), max(arr2)))    

    nbins = (bins(arr1), bins(arr2))

    h = Histogram2D('now2d', 
                    nbins=nbins,
                    aranges=arange,
                    xlabel='none',
                    ylabel='none',
                    xunits='',
                    yunits='')
    
    h.fill(zip(arr1, arr2))

    return h

def bins(arr):
    return 100
            
